package com.zoe.app.intercept;

import cn.hutool.core.io.IoUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zoe.app.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.charset.Charset;

/**
 * @author zoe.Zhang
 * @date 2022/06/09
 * @description
 */
@Slf4j
public class UserIntercept implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {
        if (request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE)) {
            ServletInputStream inputStream = request.getInputStream();
            String read = IoUtil.read(inputStream, Charset.defaultCharset());
            User user = new ObjectMapper().readValue(read, User.class);
            log.info("拦截器读取数据===>{}", user);
        }
        return true;
    }
}
